package cn.kgc.service.impl;

import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.UserContextHolder;
import cn.kgc.constant.WmsPurchaseStatus;
import cn.kgc.dto.add.WmsPurchaseAddDTO;
import cn.kgc.dto.add.WmsPurchaseDetailAddDTO;
import cn.kgc.entity.WmsPurchase;
import cn.kgc.entity.WmsPurchaseDetail;
import cn.kgc.entity.WmsWareInfo;
import cn.kgc.entity.WmsWareSku;
import cn.kgc.mapper.WmsPurchaseDetailMapper;
import cn.kgc.mapper.WmsPurchaseMapper;
import cn.kgc.mapper.WmsWareInfoMapper;
import cn.kgc.mapper.WmsWareSkuMapper;
import cn.kgc.service.WmsPurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p></p>
 *
 * @author 李孟鸠
 * @Date: 2022年12月07日11:37
 */
@Service("wmsPurchaseService")
public class WmsPurchaseServiceImpl extends ServiceImpl<WmsPurchaseMapper, WmsPurchase> implements WmsPurchaseService {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;
    @Resource
    private WmsPurchaseDetailMapper wmsPurchaseDetailMapper;

    @Resource
    private WmsWareSkuMapper wmsWareSkuMapper;

    @Resource
    private WmsWareInfoMapper wmsWareInfoMapper;




    @Override
    @Transactional
    public boolean saveWmsPurchase(WmsPurchaseAddDTO purchaseAddDTO) {
      //1 新建采购单
        WmsPurchase wmsPurchase=new WmsPurchase();
        BeanUtils.copyProperties(purchaseAddDTO,wmsPurchase);

        //采购单状态：新建状态
        wmsPurchase.setStatus(WmsPurchaseStatus.NEW.getStatus());
        wmsPurchaseMapper.insert(wmsPurchase);


        //2 新建采购单的详情页
        List<WmsPurchaseDetailAddDTO> wmsPurchaseDetailAddDTOList =
                purchaseAddDTO.getWmsPurchaseDetailAddDTOList();

        //采购单的总价
        BigDecimal totalPrice=new BigDecimal(0);
        for (WmsPurchaseDetailAddDTO wmsPurchaseDetailAddDTO : wmsPurchaseDetailAddDTOList) {
            WmsPurchaseDetail wmsPurchaseDetail=new WmsPurchaseDetail();
            BeanUtils.copyProperties(wmsPurchaseDetailAddDTO,wmsPurchaseDetail);
            wmsPurchaseDetail.setPurchaseId(wmsPurchase.getId());
            BigDecimal nums=new BigDecimal(wmsPurchaseDetailAddDTO.getSkuNum());
            BigDecimal tempPrice = wmsPurchaseDetailAddDTO.getSkuPrice().multiply(nums);
            totalPrice=totalPrice.add(tempPrice);
            wmsPurchaseDetailMapper.insert(wmsPurchaseDetail);
        }



        //采购单的总价
        wmsPurchase.setAmount(totalPrice);
        wmsPurchaseMapper.updateById(wmsPurchase); //更新总价
        return true;
    }


    @Transactional
    public boolean allocationWmsPurchase(long assigneeId, String assigneeName, String phone, long id) {
        WmsPurchase wmsPurchase = wmsPurchaseMapper.selectById(id);
        if (wmsPurchase==null){
            throw  new HttpException(40001,500);
        }

        wmsPurchase.setAssigneeId(assigneeId);
        wmsPurchase.setAssigneeName(assigneeName);
        wmsPurchase.setPhone(phone);
        //已经分配
        wmsPurchase.setStatus(WmsPurchaseStatus.ALLOCATION.getStatus());

        //分配数据
        return wmsPurchaseMapper.updateById(wmsPurchase)>0;

    }

    @Override
    public boolean finishWmsPurchase(Long id) {
        String userId="2";
        WmsPurchase wmsPurchase = wmsPurchaseMapper.selectById(id);

        if (wmsPurchase==null){
            throw  new HttpException(40003,500);
        }
        String  assigneeId = wmsPurchase.getAssigneeId()+"";

        //1 如果采购单状态
        if (wmsPurchase.getStatus()!=WmsPurchaseStatus.SHOPPING.getStatus()){
            throw  new HttpException(40006,500);
        }

        //2 完成采购的人和分配的人不是同一个人，不能够进行完成操作
        if (!userId.equals(assigneeId)){
            throw  new HttpException(40006,500);
        }

        //3修改采购单状态
        wmsPurchase.setStatus(WmsPurchaseStatus.FINISH.getStatus());
        wmsPurchaseMapper.updateById(wmsPurchase);
        long wareId=wmsPurchase.getWareId();
        //4 商品入库(查询商品的详情信息，sku)
        LambdaQueryWrapper<WmsPurchaseDetail>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmsPurchaseDetail::getPurchaseId,id);
        List<WmsPurchaseDetail> wmsPurchaseDetails =
                wmsPurchaseDetailMapper.selectList(lambdaQueryWrapper);
        //遍历采购单详情，将采购单详情入库
        for (WmsPurchaseDetail wmsPurchaseDetail : wmsPurchaseDetails) {

            Long skuId = wmsPurchaseDetail.getSkuId();
           int skuNum = wmsPurchaseDetail.getSkuNum().intValue();
            //入库的商品原来在仓库中不存在，执行新增
            LambdaQueryWrapper<WmsWareSku>wmsWareSkuLambdaQueryWrapper=
                    new LambdaQueryWrapper<>();
            wmsWareSkuLambdaQueryWrapper.eq(WmsWareSku::getWareId,wareId);
            wmsWareSkuLambdaQueryWrapper.eq(WmsWareSku::getSkuId,skuId);
            int count = wmsWareSkuMapper.selectCount(wmsWareSkuLambdaQueryWrapper);
            if (count==0){
                WmsWareSku wmsWareSku=new WmsWareSku();
                wmsWareSku.setSkuName(wmsPurchaseDetail.getSkuName());
                wmsWareSku.setSkuId(skuId);
                wmsWareSku.setWareId(wareId);
                wmsWareSku.setStock(skuNum);
                wmsWareSku.setStockLocked(0);
                wmsWareSku.setWareId(wareId);
                wmsWareSkuMapper.insert(wmsWareSku);

            }else {

                //入库的商品原来在仓库中存在，执行修改
                WmsWareSku wmsWareSku =
                        wmsWareSkuMapper.selectOne(wmsWareSkuLambdaQueryWrapper);
                wmsWareSku.setStock(wmsWareSku.getStock()+skuNum);
                wmsWareSkuMapper.updateById(wmsWareSku);
            }

        }
        return true;

    }





    @Override
    @Transactional
    public boolean acceptWmsPurchase(long id) {
        //获得登录的用户的ID
//        String userId= UserContextHolder.getInstance().getUserId();
        String userId="2";
        WmsPurchase wmsPurchase = wmsPurchaseMapper.selectById(id);
        if (wmsPurchase==null){
            throw  new HttpException(40003,500);
        }
        //1 如果采购单的状态是已分配，才能接收
        if (wmsPurchase.getStatus()!=WmsPurchaseStatus.ALLOCATION.getStatus()){
          throw  new HttpException(40004,500);
        }
        String assigneeId=wmsPurchase.getAssigneeId()+"";
        //2 接收采购单的人和分配的人是否是同一个人
        if (!userId.equals(assigneeId)){
            throw new HttpException(40005,500);
        }


        //3 更改状态(正在采购中)
        wmsPurchase.setStatus(WmsPurchaseStatus.SHOPPING.getStatus());
        //3 更改状态
        return wmsPurchaseMapper.updateById(wmsPurchase)>0;
    }
}

